﻿using System;
using System.Windows;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;

using SBPweb.MediaCenter.Common.Services;

namespace SBPweb.MediaCenter.Tools.WPF.Controls
{

    public class LayoutManager<TList> where TList : IList
    {

        #region Private fields

        private Window window;
        private Panel dropVisual;
        private TimeSpan animationSpeed = TimeSpan.FromSeconds(1);

        protected TList list;

        #endregion

        #region Constructor logic

        public LayoutManager(Window parentWindow, TList list)
            : this(parentWindow, null, list)
        {
        }

        public LayoutManager(Window parentWindow, Panel dropVisual, TList list)
        {
            this.window = parentWindow;
            this.dropVisual = dropVisual;
            this.list = list;
        }

        #endregion

        #region Property accessors

        public TList List
        {
            get
            {
                return list;
            }
        }

        public Panel DropVisual
        {
            get
            {
                return dropVisual;
            }
            set
            {
                dropVisual = value;
            }
        }

        public TimeSpan AnimationSpeed
        {
            get
            {
                return animationSpeed;
            }
            set
            {
                animationSpeed = value;
            }
        }

        #endregion

        #region Events

        public event EventHandler Initializing;

        public event EventHandler Initialized;

        #endregion

        #region Methods

        #region Initialization

        public void Initialize()
        {
            if (Initializing != null)
            {
                Initializing(this, EventArgs.Empty);
            }

            InitializeLayout();

            if (Initialized != null)
            {
                Initialized(this, EventArgs.Empty);
            }
        }

        protected virtual void InitializeLayout()
        {
        }

        #endregion

        #region Named layouts

        public string GetLayout()
        {
            throw new NotImplementedException();
        }

        public void SetLayout(string layout)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region Items managment

        protected void Add(UIElement visual)
        {
            if (Thread.CurrentThread != visual.Dispatcher.Thread)
            {
                visual.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate { Add(visual); });
            }
            else
            {
                dropVisual.Children.Add(visual);
                BringToFront(visual);
            }
        }

        protected void Remove(UIElement visual)
        {
            if (Thread.CurrentThread != visual.Dispatcher.Thread)
            {
                visual.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate { Remove(visual); });
            }
            else
            {
                dropVisual.Children.Remove(visual);
                //SelectedIndex = List.Count - 1;
            }
        }

        protected void BringToFront(List<UIElement> visuals)
        {
            foreach (UIElement visual in visuals)
            {
                BringToFront(visual);
            }
        }
        
        protected void BringToFront(UIElement visual)
        {
            if (Thread.CurrentThread != visual.Dispatcher.Thread)
            {
                visual.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate { BringToFront(visual); });
            }
            else
            {
                BringVisualToFront(visual);
                //int i = dropVisual.Children.IndexOf(visual);
                //visual.Visibility = Visibility.Visible;
                //dropVisual.Children.Add(visual);
                //SelectedIndex = List.Count - 1;
            }
        }

        protected void SendToBack(List<UIElement> visuals)
        {
            foreach (UIElement visual in visuals)
            {
                SendToBack(visual);
            }
        }

        protected void SendToBack(UIElement visual)
        {
            if (Thread.CurrentThread != visual.Dispatcher.Thread)
            {
                visual.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate { SendToBack(visual); });
            }
            else
            {
                SendVisualToBack(visual);
                //int i = dropVisual.Children.IndexOf(visual);
                //visual.Visibility = Visibility.Hidden;
                //dropVisual.Children.Add(visual);
                //SelectedIndex = List.Count - 1;
            }
        }

        #endregion

        #region Animations

        private void BringVisualToFront(UIElement visual)
        {
            DoubleAnimation anim = new DoubleAnimation(1, new Duration(TimeSpan.FromMilliseconds(1000)));
            visual.BeginAnimation(UIElement.OpacityProperty, anim);
        }

        private void SendVisualToBack(UIElement visual)
        {
            DoubleAnimation anim = new DoubleAnimation(0, new Duration(TimeSpan.FromMilliseconds(1000)));
            visual.BeginAnimation(UIElement.OpacityProperty, anim);
        }

        #endregion

        #endregion

    }

}